package com.sh.shanhuo.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Handler;
import android.text.Html;
import android.text.Spanned;
import android.text.TextUtils;
import android.text.method.ScrollingMovementMethod;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.sh.shanhuo.R;
import com.sh.shanhuo.activity.WebActivity;
import com.sh.shanhuo.bean.PushDetailListBean;
import com.sh.shanhuo.database.web.WebInfoDBUtils;
import com.sh.shanhuo.database.web.WebInfoEntity;
import com.sh.shanhuo.utils.ScreenSwitch;

import java.io.IOException;
import java.net.URL;
import java.util.List;

public class PushDetailAdapter extends BaseQuickAdapter<PushDetailListBean.DataBean, BaseViewHolder> {

    private final Context context;

    public PushDetailAdapter(Context context, List<PushDetailListBean.DataBean> dataBeans) {
        super(R.layout.push_detail_view, dataBeans);
        this.context = context;
    }

    @SuppressLint("UseCompatLoadingForDrawables")
    @Override
    protected void convert(BaseViewHolder helper, PushDetailListBean.DataBean item) {
        ImageView logo = helper.getView(R.id.logo);
        ImageView add = helper.getView(R.id.add);
        TextView title = helper.getView(R.id.title);
        TextView tryAct = helper.getView(R.id.tryAct);
        TextView content = helper.getView(R.id.content);
        TextView introductionContent = helper.getView(R.id.introductionContent);

        WebInfoEntity webInfoEntity = WebInfoDBUtils.getWebInfoByWebId(context, item.getId());

        if (webInfoEntity == null || webInfoEntity.deleteFlag == 1) {
            add.setBackground(context.getResources().getDrawable(R.drawable.icon_add, null));
            add.setClickable(true);
        } else {
            add.setBackground(context.getResources().getDrawable(R.drawable.icon_added, null));
            add.setClickable(false);
        }

        title.setText(item.getWebName());
        content.setText(item.getIntroduce());
        Glide.with(context).load(item.getLogoUrl())
                .placeholder(R.drawable.default_logo)
                .error(R.drawable.default_logo)
                .into(logo);
        if (TextUtils.isEmpty(item.getAbstraction())) {
            introductionContent.setText(item.getIntroduce());
        } else {
            final Spanned[] spanned = new Spanned[1];
            Handler handler = new Handler(msg -> {
                if (msg.what == 100) {
                    introductionContent.setText(spanned[0]);
                }
                return false;
            });
            new Thread(() -> {
                Html.ImageGetter imageGetter = this::getDrawable;
                spanned[0] = Html.fromHtml(item.getAbstraction(), imageGetter, null);
                handler.sendEmptyMessage(100);
            }).start();
        }
        add.setOnClickListener(v -> {
            WebInfoEntity webInfoEntity1 = new WebInfoEntity();
            webInfoEntity1.webId = item.getId();
            webInfoEntity1.description = item.getIntroduce();
            webInfoEntity1.sortName = "默认";
            webInfoEntity1.onTop = 0;
            webInfoEntity1.serverTop = item.getTop();
            webInfoEntity1.isInServer = 1;
            webInfoEntity1.webLink = item.getWebUrl();
            webInfoEntity1.webName = item.getWebName();
            webInfoEntity1.icon = item.getLogoUrl();
            webInfoEntity1.updateTime = System.currentTimeMillis();
            WebInfoDBUtils.insertWebInfo(context, webInfoEntity1);
            add.setBackground(context.getResources().getDrawable(R.drawable.icon_added, null));
            add.setClickable(false);
        });

        tryAct.setOnClickListener(v -> {
            if (TextUtils.isEmpty(item.getWebUrl())) {
                return;
            }
            Bundle bundle = new Bundle();
            bundle.putString("url", item.getWebUrl());
            bundle.putString("id", item.getId());
            ScreenSwitch.startActivity(context, WebActivity.class, bundle);
        });
    }

    private Drawable getDrawable(String source) {
        Drawable drawable = null;
        try {
            drawable = Drawable.createFromStream(new URL(source).openStream(), "image.jpg");
        } catch (IOException e) {
            e.printStackTrace();
        }
        if (drawable != null) {
            drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
        }
        return drawable;
    }
}
